<template>
	<view class="content">
		<u-navbar bgColor="transparent" titleStyle="color:#fff" title="编辑资料" leftIconColor="#fff" :autoBack="true"
			:placeholder="true">
		</u-navbar>
		<view class="head">
			<image :src="info.u_img" mode=""></image>
			<view class="modify">
				<image src="../../../static/my/camera.png" mode=""></image>
				<text @click="upload">更改头像</text>
			</view>
		</view>
		<view class="nickname">
			<u--input color="#fff" placeholder="请输入您的昵称" border="none" v-model="nickname"></u--input>
		</view>
		<view class="btn">
			<u-button size="large" class="btn-color" shape="circle" @click="gobuy"
				color="linear-gradient( 90deg, #C8F673 0%, #98F083 100%)" type="primary" text="保存"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				nickname: '',
				info: {}
			}
		},
		onLoad() {
			this.getUser()
		},
		methods: {
			gobuy() {
				if (this.nickname == '') {
					uni.showToast({
						title:'昵称不能为空',
						icon:'none'
					})
					return
				}
				uni.showLoading({
					mask:true
				})
				let params = {
					name:this.nickname,
					receive_qrcode:this.info.u_img.replace(/https?:\/\/gc\.yichucbb\.com/, '')
				}
				this.$Request({
					method: 'POST',
					url: '/api/personal/changename',
					data: params
				}).then(res => {
					if (res.code == 1) {
						this.$Request({
							method: 'POST',
							url: '/api/personal/changevoucher',
							data: params
						}).then(res => {
							uni.hideLoading()
							if (res.code == 1) {
								uni.showToast({
									title:'修改成功！',
									icon:'none'
								})
								let userinfo = uni.getStorageSync('info');
								userinfo.u_img = params.receive_qrcode
								userinfo.user = params.name
								uni.setStorageSync('info',userinfo)
							}else{
								uni.showToast({
									title:res.msg,
									icon:'none'
								})
							}
						})
					}else{
						uni.hideLoading()
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			},
			upload() {
				let _self = this;
				//第一步:打开手机相册或者文件管理器选择文件
				uni.chooseImage({
					count: 1, //允许上传一张图片
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						//获得选择好的文件,就算只有一个文件也是数组
						const tempFilePaths = res.tempFilePaths;
						uni.showLoading()
						uni.uploadFile({
							url: '/api/upload/upload', //后端接口url
							filePath: tempFilePaths[0],
							header: {
								'Access-Control-Allow-Origin': '*',
							},
							data: {
								token: uni.getStorageSync("token") || ''
							},
							name: 'file', //和后端接口需要的参数名一致
							success: (res) => {
								_self.info.u_img = 'https://web.sunsk.top' + JSON.parse(res.data)
									.url
								uni.showToast({
									title: "成功",
									icon: "none"
								})
							},
							complete: (ok) => {
								uni.hideLoading()
							}
						})
					}
				})
			},
			getUser() {
				let params = {

				}
				this.$Request({
					method: 'POST',
					url: '/api/Personal/myIndex',
					data: params
				}).then(res => {
					if (res.code == 1) {
						this.info = res.data.user
						this.nickname = this.info.username
					}
				})
			},
		}
	}
</script>

<style>
	page {
		background-color: #17191A;
	}

	.btn-color {
		color: #17191A !important;
		font-weight: bold;
	}
</style>
<style lang="scss" scoped>
	.content {
		min-height: 100vh;
		background-image: url('../../../static/my/my-bg.png');
		background-repeat: no-repeat;
		background-size: 100% 558rpx;
		padding: 30rpx;

		.head {
			text-align: center;
			position: relative;
			margin-top: 60rpx;

			image {
				width: 220rpx;
				height: 220rpx;
				border-radius: 50%;
			}

			.modify {
				width: 190rpx;
				background: linear-gradient(90deg, #C8F673 0%, #98F083 100%);
				padding: 10rpx 20rpx;
				border-radius: 30rpx;
				font-size: 28rpx;
				color: #17191a;
				display: flex;
				align-items: center;
				justify-content: center;
				position: absolute;
				bottom: -20rpx;
				left: 0;
				right: 0;
				margin: 0 auto;

				image {
					width: 29.28rpx;
					height: 27.08rpx;
					margin-right: 10rpx;
				}
			}
		}

		.nickname {
			padding: 30rpx;
			background-color: rgba(255, 255, 255, 0.12);
			border-radius: 40rpx;
			margin-top: 100rpx;
		}

		.btn {
			margin-top: 60rpx;
		}
	}
</style>